package com.trade.module.base.repository;

import com.trade.module.base.entity.Resource;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

/**
 * Class Description: 资源数据持久层
 *
 * @Version v1.0 2017/4/22 13:42
 * @JDK version 1.8.0_51
 * @Author zfb
 */
@Repository
public interface ResourceRepository extends PagingAndSortingRepository<Resource,String>,
        JpaSpecificationExecutor<Resource>{

    /**
     * 根据角色id和客户端id查询资源集合
     *
     * @param roleId 角色id
     * @param dataFlag 删除状态
     * @param resRoleDataFlag 资源角色删除状态
     * @return List<Resource> 资源集合
     */
    @Query(value = "FROM Resource r WHERE r.id IN (SELECT rr.resourceId FROM ResourceRole " +
            "rr WHERE rr.roleId=?1 AND rr.dataFlag=?3) AND r.dataFlag=?2")
    List<Resource> findResByRoleId(String roleId,String dataFlag, String resRoleDataFlag);

    /**
     * 根据Id集合查询资源
     *
     * @param ids id集合
     * @param dataFlag 數據刪除狀態
     * @return List<Resource>
     */
    List<Resource> findByIdInAndDataFlagOrderByCreateTimeAsc(Set<String> ids, String dataFlag);

    /**
     * Method description：根据资源父节点查询资源集合
     *
     * @param resId 资源父级ID
     * @return List<Resource>
     */
    @Query("from Resource r WHERE r.resParentResourceId=?")
    List<Resource> findAllResByParentId(String resId);
}
